package com.study.cloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.study.cloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/consumer/")
@Slf4j
@DefaultProperties(defaultFallback = "globalFallbackMethod")
public class OrderHystrixController {


    @Autowired
    private PaymentHystrixService paymentHystrixService;


    @RequestMapping(value = "/payment/hystrix/ok/{id}", method = RequestMethod.GET)
    public String paymentInfo_ok(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentInfo_ok(id);
    }

    @RequestMapping(value = "/payment/hystrix/error/{id}", method = RequestMethod.GET)
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
//    })
    @HystrixCommand
    public String paymentInfo_error(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentInfo_error(id);
    }

    //异常处理
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
        return "================>系统繁忙，稍后再试！";
    }

    // 全局falllback
    public String globalFallbackMethod() {
        return "Global异常处理信息，请稍后再试";
    }
}
